﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KipmeHQ;

public partial class Items : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected override void OnPreRender(EventArgs e)
    {
        try
        {
            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }
            string param = Request.QueryString["dept"];
            if (String.IsNullOrEmpty(param))
                Response.Redirect("Default.aspx");

            int deptID = 0;
            if (!int.TryParse(param, out deptID))
                Response.Redirect("Default.aspx");

            List<product> products = new List<product>();
            if (!Page.IsPostBack)
            {
                service.setFocusOnDepartment(deptID);

                List<category> productsCategories = new List<category>();
                productsCategories.Add(new category());
                productsCategories.Reverse();
                productsCategories.AddRange(service.getCategories());
                CategoryFilterDropDownList.DataSource = productsCategories;
                CategoryFilterDropDownList.DataBind();

                List<productType> productTypes = new List<productType>();
                productTypes.Add(new productType());
                productTypes.Reverse();
                productTypes.AddRange(service.getProductTypes());
                ddlTypesFilter.DataSource = productTypes;
                ddlTypesFilter.DataBind();

                List<productType> productsTypes = new List<productType>();
                productsTypes.Add(new productType());
                productsTypes.Reverse();
                productsTypes.AddRange(service.getProductTypes());


                foreach (productType item in productsTypes)
                {
                    if (item.typeCode != null)
                    {
                        var temp = service.getDepartmentProductsByType(item.typeCode);
                        if (temp != null)
                            products.AddRange(temp);
                    }
                }

                itemsList.DataSource = products;
                itemsList.DataBind();
            }
        }
        catch (System.Exception ex)
        {
            if ((Application["ThrowExceptions"] as bool?) == true)
                throw ex;
        }

        base.OnPreRender(e);
    }
    protected void btnApplyFilter_Click(object sender, EventArgs e)
    {
        try
        {
            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }

            string param = Request.QueryString["dept"];
            if (String.IsNullOrEmpty(param))
                Response.Redirect("Default.aspx");

            int deptID = 0;
            if (!int.TryParse(param, out deptID))
                Response.Redirect("Default.aspx");

            List<product> products = new List<product>();
            foreach (var cat in service.getCategories())
            {
                var prod = service.getDepartmentProductsByCategory(cat.categoryCode);
                if (prod != null)
                    products.AddRange(prod);
            }


            if (CategoryFilterDropDownList.SelectedIndex != 0)
            {
                products = products.Where(x => x.category.Equals(CategoryFilterDropDownList.SelectedValue)).ToList();
            }
            else if(ddlTypesFilter.SelectedIndex != 0)
            {
                products = products.Where(x => x.type.typeCode == ddlTypesFilter.SelectedValue).ToList();
            }

            itemsList.DataSource = products;
            itemsList.DataBind();
        }
        catch (System.Exception ex)
        {
            if ((Application["ThrowExceptions"] as bool?) == true)
                throw ex;
        }
    }
    protected void btnClearFilters_Click(object sender, EventArgs e)
    {
        try
        {
            HeadquarterWSClient service = Application["KipmeHQWS"] as HeadquarterWSClient;
            if (service == null)
            {
                service = new HeadquarterWSClient();
            }

            string param = Request.QueryString["dept"];
            if (String.IsNullOrEmpty(param))
                Response.Redirect("Default.aspx");

            int deptID = 0;
            if (!int.TryParse(param, out deptID))
                Response.Redirect("Default.aspx");

            List<product> products = new List<product>();
            foreach (var cat in service.getCategories())
            {
                var prod = service.getDepartmentProductsByCategory(cat.categoryCode);
                if (prod != null)
                    products.AddRange(prod);
            }

            itemsList.DataSource = products;
            itemsList.DataBind();
            CategoryFilterDropDownList.SelectedIndex = 0;
            ddlTypesFilter.SelectedIndex = 0;
        }
        catch (System.Exception ex)
        {
            if ((Application["ThrowExceptions"] as bool?) == true)
                throw ex;
        }
    }
}